package com.cloudptt.wp.ui.views;

import com.cloudptt.api.product.func.call.single.LaunchCallInfo;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.wp.R;
import com.cloudptt.wp.ui.dlg.CallInfoDlg;
import com.mcptt.db.model.AddrInfo;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class GroupcallStateLayout extends LinearLayout implements View.OnClickListener {

	private TextView mNameTv;
	private TextView mStateTv;
	private ImageView voiceFrameAnim;
	private AnimationDrawable animation = null;

	private AddrInfo peerAddrInfo = null;
	private LaunchCallInfo info = null;
	private CallInfoDlg dlg = null;
	
	public GroupcallStateLayout(Context context) {
		this(context, null);
		// TODO Auto-generated constructor stub
	}

	public GroupcallStateLayout(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
		// TODO Auto-generated constructor stub
	}

	public GroupcallStateLayout(Context context, AttributeSet attrs, int defStyleAttr) {
		this(context, attrs, defStyleAttr, 0);
		// TODO Auto-generated constructor stub
	}

	public GroupcallStateLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
		super(context, attrs, defStyleAttr, defStyleRes);
		// TODO Auto-generated constructor stub
		LayoutInflater.from(context).inflate(R.layout.layout_group_call_state, this);

		FrameLayout allLy = (FrameLayout)findViewById(R.id.allLy);
		allLy.setOnClickListener(this);
		ImageView callCountIv= (ImageView)findViewById(R.id.callCountIv);
		callCountIv.setOnClickListener(this);
		mNameTv = (TextView)findViewById(R.id.groupNameTv);
		mStateTv = (TextView)findViewById(R.id.callStatusTv);
		voiceFrameAnim = (ImageView)findViewById(R.id.voice_frame_anim);
	}

	public void setCallInfo(AddrInfo peerAddrInfo,LaunchCallInfo info)
	{
		this.peerAddrInfo = peerAddrInfo;
		this.info = info;
	}

	private void showVoiceFrameAnimByXml() {
		stopAnim();
		// 设置图像视图的图像来源为帧动画的XML定义文件
		voiceFrameAnim.setImageResource(R.drawable.animation_list_ptt_voice);
		// 从图像视图对象中获取帧动画
		animation = (AnimationDrawable) voiceFrameAnim.getDrawable();
		animation.start(); // 开始播放帧动画
	}

	private void showIdelFrameAnimByXml() {
		stopAnim();
		// 设置图像视图的图像来源为帧动画的XML定义文件
		voiceFrameAnim.setImageResource(R.drawable.animation_list_ptt_idle);
		// 从图像视图对象中获取帧动画
		animation = (AnimationDrawable) voiceFrameAnim.getDrawable();
		animation.start(); // 开始播放帧动画
	}
	public void stopAnim()
	{
		if(animation != null)
		{
			animation.stop();
		}
	}
	/*public TextView getNameView(){
		return mNameTv;
	} 
	
	public TextView getStateView(){
		return mStateTv;
	}*/
	
	/*public boolean isNameShown(){
		return mNameTv.isShown();
	}*/
	
	public void setGroupName(String name){
		Log.debug(GroupcallStateLayout.class, "setGroupName name = " + name);
		if(mNameTv != null){
			mNameTv.setText(name);
		}

	}
	
	public void setState(String callstate){
		if(mStateTv != null){
			mStateTv.setText(callstate);
		}
	}

	public void updateHasVoice(boolean hasVoice)
	{
		if(hasVoice)
		{
			showVoiceFrameAnimByXml();
		}
		else
		{
			showIdelFrameAnimByXml();
		}
	}

	public void updateTimer(String timerStr){
//		if(mTimertv != null){
//			mTimertv.setText(timerStr);
//		}
	}

	public void cleanTimer(){
//		if(mTimertv != null){
//			mTimertv.setText("");
//		}
		stopAnim();
	}
	
	public void resetView(){
		mNameTv.setText("");
		mStateTv.setText("");
//		mTimertv.setText("");
		stopAnim();
	}

	@Override
	public void onClick(View view) {
		if(R.id.allLy == view.getId()
			|| R.id.callCountIv == view.getId())
		{
			dlg = new CallInfoDlg(getContext(),peerAddrInfo,info,true);
			dlg.show();
		}
	}

	public void onCallEnd(String callKey)
	{
		if(dlg != null)
		{
			dlg.dismiss();
		}
	}
}
